<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机点名</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }

        .container {
            text-align: center;
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        h1 {
            font-size: 24px;
            margin-bottom: 20px;
        }

        .name-display {
            font-size: 28px;
            font-weight: bold;
            margin-bottom: 20px;
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 5px;
            min-height: 40px;
            background-color: #fafafa;
        }

        button {
            font-size: 16px;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            margin: 5px;
        }

        button:hover {
            background-color: #45a049;
        }

        #stopButton {
            background-color: #f44336;
        }

        #stopButton:hover {
            background-color: #e53935;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>随机点名</h1>
        <div class="name-display" id="nameDisplay">请点击开始按钮点名</div>
        <button id="startButton">开始</button>
        <button id="stopButton" disabled>结束</button>
    </div>

    <script>
        // 姓名列表
        const names = ["张三", "李四", "王五", "赵六", "钱七"];

        // 获取HTML元素
        const nameDisplay = document.getElementById("nameDisplay");
        const startButton = document.getElementById("startButton");
        const stopButton = document.getElementById("stopButton");

        let count = 0

        let timer = null;

        // 点击 开始按钮 事件器
        startButton.addEventListener("click", () => {
            if (names.length === 1) {
                nameDisplay.innerText = names[0]
                startButton.disabled = true;
                stopButton.disabled = true;
            }
            else {
                startButton.disabled = true;
                stopButton.disabled = false;
                if (count === names.length) {
                    count = 0
                    nameDisplay.innerText = names[count]
                }
                else {
                    nameDisplay.innerText = names[count]
                }
                timer = setInterval(() => {
                    count++
                    if (count === names.length) {
                        count = 0
                    }

                    if (count > names.length) {
                        nameDisplay.innerText = names[count - 1]
                    }

                    else {
                        nameDisplay.innerText = names[count]
                    }
                }, 500);
            }
        });

        // 点击 结束按钮 事件器
        stopButton.addEventListener("click", () => {

            clearInterval(timer);

            // 从数组中删除该名字
            names.splice(count, 1);

            startButton.disabled = false;
            stopButton.disabled = true;
        });


    </script>
</body>

</html>